import router from "@/router";
import axios from "axios";
import { Message } from "element-ui";
// import {} from "../untils/loading.js"
// import router from '@/router/index'
const baseurl = "http://110.41.19.205:8080";

const service = axios.create({
  // baseURL: global.baseUrl,
  baseURL: baseurl,
  timeout: 5000, // 请求超时
});

// 请求拦截器
service.interceptors.request.use(
    (config) => {
    const loginkey = localStorage.getItem("loginKey");
    if (loginkey) {
      config.headers.satoken = loginkey;
    }
    console.log("sevice-request:", config);
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  (Response) => {
    console.log("Response", Response);
    const { data, status, statusText } = Response;
    if (status === 200) {
      const { code, success, message, resultObj } = data;
      if (success) {
        return Promise.resolve(resultObj);
      } else {
        console.log("data--", data);
        if (code === 401) {
          router.push("/login");
        } else {
          Message.error(message);
          return Promise.reject("");
        }
      }
    } else {
      Message.error(statusText);
      return Promise.reject("");
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);
export default service;
